package org.rowan.infovis.soccer;

import com.sun.opengl.util.texture.Texture;
import com.sun.opengl.util.texture.TextureCoords;
import javax.media.opengl.GL;

public class TacticalUtility {

    private Texture ball;
    private final float WIDTH = 25;
    private final float HEIGHT = 25f;
    private final float PADDING = 1f;
    private int value;

    public TacticalUtility(Texture ball, int value) {
        this.ball = ball;
        this.value = value;
    }

    public void draw(GL gl) {
        int rowCount = 0;
        int colCount = 0;

        for (int i = 0; i < value; i++) {
            gl.glPushMatrix();
            gl.glTranslated(rowCount * WIDTH + PADDING, -1 * colCount * HEIGHT + PADDING, 0);
            drawSoccerBall(gl);
            gl.glPopMatrix();
            rowCount++;

            if (rowCount == 8) {
                rowCount = 0;
                colCount++;
            }
        }
    }

    private void drawSoccerBall(GL gl) {
        gl.glColor3f(1f, 1f, 1f);

        gl.glEnable(gl.GL_BLEND);
        gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA);

        TextureCoords tc = ball.getImageTexCoords();
        float tx1 = tc.left();
        float ty1 = tc.bottom();
        float tx2 = tc.right();
        float ty2 = tc.top();

        ball.enable();
        ball.bind();

        gl.glPushMatrix();
        gl.glBegin(GL.GL_QUADS);
        gl.glTexCoord2f(tx1, ty1);
        gl.glVertex2f(0f, 0f);
        gl.glTexCoord2f(tx1, ty2);
        gl.glVertex2f(0f, HEIGHT);
        gl.glTexCoord2f(tx2, ty2);
        gl.glVertex2f(WIDTH, HEIGHT);
        gl.glTexCoord2f(tx2, ty1);
        gl.glVertex2f(WIDTH, 0f);
        gl.glEnd();
        gl.glPopMatrix();

        ball.disable();

        gl.glDisable(gl.GL_BLEND);
    }
}
